package yijy.company;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by yijy on 2017/5/4.
 */
public class EvenChecker implements Runnable {
    private IntGennerator gennerator;
    private final int id;

    public EvenChecker(IntGennerator gennerator, int id) {
        this.gennerator = gennerator;
        this.id = id;
    }

    @Override
    public void run() {
        while(!gennerator.isCanceled()){
            int val = gennerator.next();
            if(val % 2 !=0){
                System.out.println(val + " not even!");
                gennerator.cancel();
            }
        }
    }

    public static void test(IntGennerator gp, int count){
        System.out.println("Press Control-C to exit");
        ExecutorService exec = Executors.newCachedThreadPool();
        for(int i=0; i<count; i++){
            exec.execute(new EvenChecker(gp,i));
        }
        exec.shutdown();
    }

    public static void test(IntGennerator gp){
        test(gp,10);
    }
}
